package com.sanjiahantang.youjiarole.module.login.views.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.dialog.CommonDialog;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseApplication;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.apk.ApkDownloadUtils;
import com.sanjiahantang.youjiarole.module.common.presenters.MainPresenter;
import com.sanjiahantang.youjiarole.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiarole.module.login.presenters.LoginPresenter;
import com.sanjiahantang.youjiarole.module.login.views.LoginView;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.umeng.socialize.UMShareAPI;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2015/8/31.
 * 登陆
 */
public class LoginActivity extends BaseMvpActivity<LoginView, LoginPresenter> implements LoginView, View.OnClickListener, TextWatcher {


    private BaseMvpActivity context;
    private EditText editPassword;
    private EditText editUserName;

    private TextView btnLogin;
    private String name;
    private String password;
    private int type;

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {

        setContentView(R.layout.activity_login);
        context = this;
        type = getIntent().getIntExtra("type", 0);

        btnLogin = (TextView) findViewById(R.id.btnLogin);
        findViewById(R.id.btnRegister).setOnClickListener(this);
        findViewById(R.id.btnForget).setOnClickListener(this);
        editPassword = (EditText) findViewById(R.id.editPassword);
        editUserName = (EditText) findViewById(R.id.editUserName);


        editUserName.addTextChangedListener(this);
        editPassword.addTextChangedListener(this);


        if (ConfigValue.isLogoutByOtherUser){

            Utils.showDialog($this, "提示", (String) ConfigValue.param, "确认", "", 1, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.dismissDialog();
                }
            });
            ConfigValue.isLogoutByOtherUser = false;
            ConfigValue.param = null;
        }

        if (ConfigValue.isLogoutByTimeOut){
            Utils.showToast($this,(String) ConfigValue.param);
            ConfigValue.isLogoutByTimeOut = false;
            ConfigValue.param = null;
        }
    }

    @Override
    public void initData() {
        ApkDownloadUtils.versionCheck(this, 0);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnLogin:
                presenter.loadLogin(context, name, password, new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        if (content instanceof Integer) {
                            if ((int) content == RESPONSE_CODE_SUC) {
                                type = 0;
                            }
                        }
                    }
                });
                break;
            case R.id.btnRegister:
                startActivity(new Intent(context, RegisterActivity.class).putExtra("type", type));
                break;
            case R.id.btnForget:
                presenter.toForget(context);
                break;
        }
    }


    @Override
    public void showProgress() {
        super.showProgress(this);
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        name = editUserName.getText().toString().trim();
        password = editPassword.getText().toString().trim();
        if (!name.equals("") && !password.equals("")) {
            btnLogin.setClickable(true);
            btnLogin.setOnClickListener(LoginActivity.this);
        } else {
            btnLogin.setClickable(false);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }

    @Override
    public void onBackPressed() {
        System.exit(0);
    }

    public static void logout(Context context){
        try {
            Set<String> set = new HashSet<>();
            JPushInterface.cleanTags(context,0);
            JPushInterface.deleteAlias(context,0);
            JPushInterface.deleteTags(context,0,set);
            JPushInterface.setTags(context, 0, set);
            JPushInterface.setAlias(context,0,"");
            ConfigValue.userInfo = null;
            ConfigValue.DATA_KEY = "";
            SPUtils.remove(context, SPConfig.PHONE);
            SPUtils.remove(context, SPConfig.KEY);
            SPUtils.remove(context, SPConfig.USERNAME);
            SPUtils.remove(context, SPConfig.ADDRESS);
            SPUtils.remove(context, SPConfig.SYS_SPOINTS);
            SPUtils.remove(context, SPConfig.SIGN_TIME);
            JPushInterface.cleanTags(context, 0);
            JPushInterface.clearLocalNotifications(context);
            JPushInterface.clearAllNotifications(context);

//            JPushInterface.stopPush(context);

            Intent mIntent = new Intent(ConfigValue.ACTION_ALTER_CARTGOODS_NUMS);
            mIntent.putExtra("cartgoodsnum", "0");
            //发送广播
            context.sendBroadcast(mIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent mIntent = new Intent(context,LoginActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(mIntent);
        BaseApplication.clearBaseActivity();
    }

}
